const {
  baseUrl,
  getRequest,
  postRequest
} = require("../../../utils/request.js")
const {
  mock_flag,
  login_flag
} = require("../../../data/index.js")
const {
  repairReportDesc_mock
} = require("../../../mock/repairReport.js")
Page({
  data: {
    //用户id
    userId: 0,
     //维修报备id
     repairReportId: '',
    // 维修报备数据
    repairReport: '',
    // 预期到达时间输入值
    expectedArrival: ''
  },
  onLoad(options) {
    //登录拦截
    const isLogin = wx.getStorageSync('isLogin');
    if (!isLogin) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      if (login_flag) {
        wx.redirectTo({
          url: '/pages/user/wxlogin/wxlogin'
        });
      }
      if (!login_flag) {
        wx.redirectTo({
          url: '/pages/user/login/login'
        });
      }
    }
    //初始化数据
    this.setData({
      userId: wx.getStorageSync('userId'), //获取用户id
      repairReportId: options.id,
    })
    //mock数据
    if (mock_flag) {
      this.setData({
        repairReport: repairReportDesc_mock
      })
    }
    //网络请求
    if (!mock_flag) {
      this.http_repairReport_findById()
    }
  },
  /**
   * 接取表单
   */
  // 处理预期到达时间输入
  handleExpectedArrivalInput(e) {
    this.setData({
      expectedArrival: e.detail.value
    });
  },
  // 处理接取任务按钮点击事件
  handleAcceptTask() {
    if (!this.data.expectedArrival) {
      wx.showToast({
        title: '请输入预期到达时间',
        icon: 'none'
      });
      return;
    }
    if(mock_flag){
      wx.showToast({
        title: '接取成功',
        icon: 'success'
      });
      wx.switchTab({url: '/pages/work/index/index',})
    }
    if(!mock_flag){
this.http_repairReport_take()
    }
  },
  /**
   * 网络请求
   */
  //查询维修报备详情
  http_repairReport_findById() {
    getRequest(baseUrl + "/front/repairReport/findById", {
      repairReportId: this.data.repairReportId
    }).then(res => {
      //console.log(res.data)
      if (res.code == 200) {
        this.setData({
          repairReport: res.data
        })
      }
    })
  },
  //接取任务
  http_repairReport_take() {
    postRequest(baseUrl + "/front/repairReport/take", {
      repairReportId:this.data.repairReportId,
      maintainerId:this.data.userId,
      repairReportExpectedArrival:this.data.repairReportExpectedArrival,
    }).then(res => {
      //console.log(res.data)
      if (res.code == 200) {
        wx.showToast({
          title: '接取成功',
          icon: 'success'
        });
        wx.switchTab({url: '/pages/work/index/index',})
      }
    })
  },
});